package com.sg.business.commons.field.value;

import com.mobnut.db.file.RemoteFile;
import com.sg.business.model.IACLClient;
import com.sg.business.model.PermissionExtendsionTest;
import com.sg.widgets.part.editor.PrimaryObjectEditorInput;
import com.sg.widgets.part.editor.fields.IFileControl;

public class PermissionControl implements IFileControl {

	public PermissionControl() {
	}

	@Override
	public boolean canDownload(PrimaryObjectEditorInput input,
			RemoteFile remoteFile) {
		return permissionTest(input, IACLClient.CODE_DOWNLOAD);
	}

	@Override
	public boolean canViewFile(PrimaryObjectEditorInput input,
			RemoteFile remoteFile) {
		return permissionTest(input, IACLClient.CODE_READ);
	}

	@Override
	public boolean canRemoveFile(PrimaryObjectEditorInput input,
			RemoteFile remoteFile) {
		return permissionTest(input, IACLClient.CODE_EDIT);
	}

	@Override
	public boolean canUploadFile(PrimaryObjectEditorInput input) {
		return permissionTest(input, IACLClient.CODE_EDIT);
	}

	private boolean permissionTest(PrimaryObjectEditorInput input,
			String permissionCode) {
		if (Boolean.TRUE.equals(input.getEnvironment("skipPermission"))) {
			return true;
		}
		boolean value = PermissionExtendsionTest.test(input.getData(),
				permissionCode);
		return value;
	}

}
